package com.mju.config;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "custom.elasticsearch")
@Data
public class ElasticConfig {		
	private String serverUrl;
	private String apikey;	
	@Bean
	public ElasticsearchClient esClient() {		
		// Create the low-level client
		RestClient restClient = RestClient
		    .builder(HttpHost.create(serverUrl))
		    .setDefaultHeaders( new Header[]{
		        new BasicHeader("Authorization", "ApiKey " + apikey)
		    }).build();		
		// Create the transport with a Jackson mapper
		ElasticsearchTransport transport = new RestClientTransport(
		    restClient, new JacksonJsonpMapper());						
		// And create the API client
		ElasticsearchClient esClient = new ElasticsearchClient(transport);
		return esClient;				
	}	
}
